//
//  YUNADViewController.m
//  BaiSi
//
//  Created by warm on 17/8/14.
//  Copyright © 2017年 hp. All rights reserved.
//

#import "YUNADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "YUNAdItem.h"
#import <UIImageView+WebCache.h>
#import "YUNViewController.h"


#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"


@interface YUNADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *adContainView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)YUNAdItem *item;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;

@property(weak,nonatomic)NSTimer *timer;



@end

@implementation YUNADViewController
- (IBAction)clickJump:(id)sender {
    
    YUNViewController *tabBarVC=[[YUNViewController alloc]init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController=tabBarVC;
    
    
    [_timer invalidate];
}

-(UIImageView *)imageView
{


if(_imageView==nil)
{



    UIImageView *imageView=[[UIImageView alloc]init];
    [self.adContainView addSubview:imageView];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [imageView addGestureRecognizer:tap];
    imageView.userInteractionEnabled=YES;
    
    _imageView=imageView;
    

}
    return _imageView;

}


-(void)tap
{



    NSURL *url=[NSURL URLWithString:_item.ori_curl];
    
    UIApplication *app=[UIApplication sharedApplication];
    
    if([app canOpenURL:url])
    {
    
        [app openURL:url];
    
    }



}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupLaunchImage];
    
    
    [self loadAdData];
    
    
    _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    
    
}
-(void)timeChange
{



    static int i=3;
    if(i==0)
    {
    
    
        [self clickJump:nil];
    
    }
    
    i--;
    
    [_jumpBtn setTitle:[NSString stringWithFormat:@"跳转(%d)",i] forState:UIControlStateNormal];

}

-(void)loadAdData
{

 

    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    
    NSMutableDictionary *paramaters=[NSMutableDictionary dictionary];
    paramaters[@"code2"]=code2;
    
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:paramaters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        [responseObject writeToFile:@"/Users/warm/Desktop/infomation.plist" atomically:YES];
        
        NSDictionary *adDict=[responseObject[@"ad"] lastObject];
        
        
        _item=[YUNAdItem mj_objectWithKeyValues:adDict];
        
        // 创建UIImageView展示图片 =>
        CGFloat h = XMGScreenW / _item.w * _item.h;
        self.imageView.frame = CGRectMake(0, 0, XMGScreenW, h);
        // 加载广告网页
        [ self.imageView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
        
        
        
        NSLog(@"%@",responseObject);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
    }];
    
    
    
    



}

-(void)setupLaunchImage
{
    // 4s:LaunchImage@2x.png
    if (iphone6P) { // 6p
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    } else if (iphone6) { // 6
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iphone5) { // 5
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
        
    } else if (iphone4) { // 4
        
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-700"];
    }
    



}

@end
